package com.ruoyi.controller.inventory;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.inventory.MesInventoryRecord;
import com.ruoyi.service.inventory.IMesInventoryRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 库存记录Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/inventory/record")
public class MesInventoryRecordController extends BaseController {
    @Autowired
    private IMesInventoryRecordService IMesInventoryRecordService;

    /**
     * 查询库存记录列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesInventoryRecord mesInventoryRecord) {
        startPage();
        List<MesInventoryRecord> list = IMesInventoryRecordService.selectMesInventoryRecordList(mesInventoryRecord);
        return getDataTable(list);
    }

    /**
     * 导出库存记录列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:record:export')")
    @Log(title = "库存记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesInventoryRecord mesInventoryRecord) {
        List<MesInventoryRecord> list = IMesInventoryRecordService.selectMesInventoryRecordList(mesInventoryRecord);
        ExcelUtil<MesInventoryRecord> util = new ExcelUtil<MesInventoryRecord>(MesInventoryRecord.class);
        util.exportExcel(response, list, "库存记录数据");
    }

    /**
     * 获取库存记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:record:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId) {
        return success(IMesInventoryRecordService.selectMesInventoryRecordByRecordId(recordId));
    }

    /**
     * 新增库存记录
     */
    @PreAuthorize("@ss.hasPermi('inventory:record:add')")
    @Log(title = "库存记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesInventoryRecord mesInventoryRecord) {
        // 校验记录编号唯一性
        if ("1".equals(IMesInventoryRecordService.checkRecordCodeUnique(mesInventoryRecord.getRecordCode(),
                mesInventoryRecord.getRecordId()))) {
            return error("新增库存记录失败，记录编号已存在");
        }
        return toAjax(IMesInventoryRecordService.insertMesInventoryRecord(mesInventoryRecord));
    }

    /**
     * 修改库存记录
     */
    @PreAuthorize("@ss.hasPermi('inventory:record:edit')")
    @Log(title = "库存记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesInventoryRecord mesInventoryRecord) {
        // 校验记录编号唯一性（排除自身）
        MesInventoryRecord existRecord = IMesInventoryRecordService
                .selectMesInventoryRecordByRecordId(mesInventoryRecord.getRecordId());
        if (existRecord != null && !existRecord.getRecordCode().equals(mesInventoryRecord.getRecordCode())) {
            if ("1".equals(IMesInventoryRecordService.checkRecordCodeUnique(mesInventoryRecord.getRecordCode(),
                    mesInventoryRecord.getRecordId()))) {
                return error("修改库存记录失败，记录编号已存在");
            }
        }
        return toAjax(IMesInventoryRecordService.updateMesInventoryRecord(mesInventoryRecord));
    }

    /**
     * 删除库存记录
     */
    @PreAuthorize("@ss.hasPermi('inventory:record:remove')")
    @Log(title = "库存记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds) {
        return toAjax(IMesInventoryRecordService.deleteMesInventoryRecordByRecordIds(recordIds));
    }

    /**
     * 校验记录编号
     */
    @PostMapping("/checkRecordCodeUnique")
    public AjaxResult checkRecordCodeUnique(@RequestBody MesInventoryRecord mesInventoryRecord) {
        String result = IMesInventoryRecordService.checkRecordCodeUnique(mesInventoryRecord.getRecordCode(),
                mesInventoryRecord.getRecordId());
        if ("1".equals(result)) {
            return error("记录编号已存在");
        }
        return success();
    }

    /**
     * 根据物料编码查询库存记录
     */
    @GetMapping("/listByMaterialCode/{materialCode}")
    public AjaxResult listByMaterialCode(@PathVariable String materialCode) {
        List<MesInventoryRecord> list = IMesInventoryRecordService.selectRecordsByMaterialCode(materialCode);
        return success(list);
    }

    /**
     * 根据仓库编码查询库存记录
     */
    @GetMapping("/listByWarehouseCode/{warehouseCode}")
    public AjaxResult listByWarehouseCode(@PathVariable String warehouseCode) {
        List<MesInventoryRecord> list = IMesInventoryRecordService.selectRecordsByWarehouseCode(warehouseCode);
        return success(list);
    }

    /**
     * 根据关联单号查询库存记录
     */
    @GetMapping("/listByRelatedCode/{relatedCode}")
    public AjaxResult listByRelatedCode(@PathVariable String relatedCode) {
        List<MesInventoryRecord> list = IMesInventoryRecordService.selectRecordsByRelatedCode(relatedCode);
        return success(list);
    }
}